# @Version        : 1.0
# @Update Time    : 2025/3/13 15:33
# @File           : profile_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息

# views/profile_view.py
import flet as ft


# views/profile_view.py
class ProfileView(ft.ListView):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.expand = True
        self.spacing = 10  # 修改: 增加间距以合理布局
        self.build_ui()

    def build_ui(self):
        # 顶部用户信息
        avatar_section = ft.Container(
            content=ft.Column(
                [
                    ft.CircleAvatar(
                        radius=40,
                        content=ft.Image(src="assets/avatar.png")  # 默认头像
                    ),
                    ft.Text("朱**", size=20, weight=ft.FontWeight.BOLD),
                    ft.Text("ceshi@email.com", color=ft.colors.GREY_600)
                ],
                spacing=8,
                horizontal_alignment=ft.CrossAxisAlignment.CENTER
            ),
            padding=20,
            bgcolor=ft.colors.GREY_50,  # 添加浅灰色背景
            border_radius=ft.border_radius.only(top_left=12, top_right=12),
            alignment=ft.alignment.center,  # 修改: 设置容器内容居中对齐
        )

        # 功能按钮区（订单/优惠券/收藏）
        function_row = ft.Container(
            content=ft.Row(
                [
                    self._build_function_btn("/order_management", ft.icons.ASSIGNMENT, "订单", ft.colors.BLUE),
                    self._build_function_btn("/coupon", ft.icons.LOCAL_OFFER, "优惠券", ft.colors.GREEN),
                    self._build_function_btn("/favorites", ft.icons.FAVORITE, "收藏", ft.colors.RED)
                ],
                alignment=ft.MainAxisAlignment.SPACE_EVENLY
            ),
            bgcolor=ft.colors.WHITE,
            padding=15,
            border_radius=12,
            margin=ft.margin.symmetric(horizontal=15, vertical=10)
        )

        # 设置选项卡片
        settings_card = ft.Container(
            content=ft.Column(
                [
                    self._build_setting_item("/", ft.icons.ACCOUNT_CIRCLE, "我的简介", ft.colors.GREEN),
                    ft.Divider(height=1),
                    self._build_setting_item("/notification", ft.icons.NOTIFICATIONS, "通知设置", ft.colors.GREEN),
                    ft.Divider(height=1),
                    self._build_setting_item("/change_password", ft.icons.LOCK, "修改密码", ft.colors.BLUE),
                    # 新增: 修改密码选项
                    ft.Divider(height=1),
                    self._build_setting_item("/login", ft.icons.EXIT_TO_APP, "退出")
                ], spacing=10  # 修改: 增加间距以合理布局
            ),
            bgcolor=ft.colors.WHITE,
            border_radius=12,
            margin=ft.margin.symmetric(horizontal=15),
            alignment=ft.alignment.center  # 新增: 设置容器内容居中对齐
        )

        self.controls = [
            avatar_section,
            function_row,
            settings_card
        ]

    def _build_function_btn(self, path, icon, text, color=ft.colors.GREY_600):  # 修改: 添加颜色参数，默认为灰色
        return ft.Column(
            [
                ft.IconButton(icon=icon, icon_size=28, icon_color=color, on_click=lambda e: self.page.go(path)),
                # 修改: 设置图标颜色
                ft.Text(text, size=14)
            ], spacing=5, horizontal_alignment=ft.CrossAxisAlignment.CENTER
        )

    def _build_setting_item(self, path, icon, text, color=ft.colors.RED_600):
        return ft.ListTile(
            leading=ft.Icon(icon, color=color),
            title=ft.Text(text, color=color),
            trailing=ft.Icon(ft.icons.CHEVRON_RIGHT, size=20),
            dense=True,
            min_vertical_padding=15,
            on_click=lambda e: self.page.go(path),
            hover_color=ft.colors.GREY_50
        )
